$(function(){

    // 课程重新编辑
    // 有录播课-显示录拨课重填影片
    // 没录拨课-显示表单预设资料填写
    var edit_class_val = '.class-course-sorts .radio-list';
    var edit_class_btn = $(edit_class_val).text();
    var select_edit=edit_class_btn.split("/");
    if(select_edit[0]=='录播课'){
        $( '.next-record-class').show(0);
        $( '.record-class-teacher').show(0);
    }
    else{
        $( '.save,.cancel').show(0);
    }
    automatic(select_edit);
    //分类自动选择
    function automatic(select_edit){
        var classify_data = "type/stage/subject";               // 课程类型/年级/学科
        var select=classify_data.split("/");                       // 分割为阵列
        for (var i = 0; i < 3; i++) {
            var compared = '.'+select[i]+' > .content > div';   //对比资料数据
            $( compared ).each(function(){
                if(select_edit[i]==$( this ).text()){              // 如果待修改的资讯与选项文字相同
                    $( compared ).removeClass("select");
                    $( this ).addClass("select");                 // 选择，并清空其他数据
                }
            });
        }
    }
    autoinput();

    // 自动输入input
    function autoinput(){
        var classify_data = "course_type/grade/subject";                 // 课程类型/年级/学科
        var check=classify_data.split("/");

        var default_val = $('.class-course-sorts .radio-list').text();  // 修改值
        var input_val = default_val.split("/");
        for (var i = 0; i < 3; i++) {
            $('[name="'+check[i]+'"]').val(input_val[i]);
        }
    }

    // 如果焦点移开-自动选
    var select_btn = '.page-recently-select select';
    $(select_btn).change(function(){
        autoselect(select_btn);
    });
    // 自行重选-每个只能则一
    var classify_data = "type/stage/subject";
    var select=classify_data.split("/");
    for (var i = 0; i < 3; i++) {
        selectclassify(select[i]);
    }
    function autoselect(select_btn){
        var str_data=$(select_btn).val();
        var str=str_data.split("/"); //字串使用/做分割

        for (var i = 0; i < 3; i++) {
            var compared = '.'+select[i]+' > .content > div';
            $( compared ).each(function(){
                if(str[i]==$( this ).text()){
                    $( compared ).removeClass("select");
                    $( this ).addClass("select");
                }
            });
        }
    }
    function selectclassify(select){
        var compared = '.'+select+' > .content > div';
        $( compared ).click(function () {
            $( compared ).removeClass("select");
            $(this).toggleClass("select");
        });
    }

    //如果按钮1点选，则展开box2
    $( ".next-wright-class" ).click(function () {
        var type_word = $('.type .select').text();
        var stage_word = $('.stage .select').text();
        var subject_word = $('.subject .select').text();
        if(!type_word){
            alert('请选择课程类型');
            return false;
        }
        if(!stage_word){
            alert('请选择适用年级')
            return false;
        }
        if(!subject_word){
            alert('请选择学科')
            return false;
        }
        if(subject_word&&type_word&&stage_word) {
            $('.class-course-sorts .radio-list').text(type_word + '/' + stage_word + '/' + subject_word);
            $("input[name='course_type']").val(type_word);
            $("input[name='grade']").val(stage_word);
            $("input[name='subject']").val(subject_word);
            $( '.form-page-2' ).slideDown();
            $( '.form-page-1' ).slideUp();
            $( '.save').show(0);
            $(this).hide(0);
            $('.portlet.light>.portlet-title>.caption>.caption-subject .title').text('课程基本信息');

            if(type_word=='录播课'){
                $( '.next-record-class').show(0);
                $( '.record-class-teacher').show(0);
            }
            else{
                $( '.save,.cancel').show(0);
            }
        }
    });

    $( ".course-edit" ).click(function () {
        $( '.save,.cancel,.next-record-class,.pre-record-class' ).hide(0);
        $( '.next-wright-class' ).show(0);
        $( '.form-page-1' ).slideDown();
        $( '.form-page-2' ).slideUp();
        $('.portlet.light>.portlet-title>.caption>.caption-subject .title').text('选择课程分类');
    });

    $( ".next-record-class" ).click(function () {
        $( '.next-record-class' ).hide(0);
        $( '.save,.cancel,.pre-record-class' ).show(0);
        $( '.form-page-2' ).slideUp();
        $( '.form-page-3' ).slideDown();
        $('.portlet.light>.portlet-title>.caption>.caption-subject .title').text('录播课上传');
    });

    $( ".pre-record-class" ).click(function () {
        $( '.save,.cancel,.next-wright-class' ).hide(0);
        $( '.next-record-class,.pre-record-class' ).show(0);
        $( '.form-page-3' ).slideUp();
        $( '.form-page-2' ).slideDown();
        $('.portlet.light>.portlet-title>.caption>.caption-subject .title').text('课程基本信息');
    });


    $("#dr_sel_price").focusout(function(){
        var num = $("#dr_sel_price").val();
        if(!num){
            if(!(/[0-9]/.test(num))){
                alert("请填写数字");
            }
            else{
                alert("请填写金额");
            }
            return false;
        }

    });
    $("#dr_del_price").focusout(function(){
        var num = $("#dr_del_price").val();
        if(!num){
            if(!(/[0-9]/.test(num))){
                alert("请填写数字");
            }
            else{
                alert("请填写金额");
            }
            return false;
        }
    });

    // 上传图片选择
    $("#load_img").change(function () {
        var link = $(this).val();
        var name=link.split("\\");
        for(var i = 0 ;i<3;i++){
            console.log(name[i]);
        }
        if(link!=''){
            $('.update-class-pic').css('cssText','background-image:url(./uploadfile/video/'+name[2]+')');
        }
    });
    if($(".update-word-add > img").length>0){
        var src = $(".update-word-add > img").attr("src");
        $(".image-data").text(src);
        $('.update-class-pic').css('cssText','background-image:url('+src+')');
        $(".update-word-add").fadeOut();
    }


    // 点击（Enter按钮），并确认信息是否正确
    $(document).keypress(function(e) {
        if(e.which == 13) {
            var dr_title = $("#dr_title").val(); // 课程名称
            var dr_aux_title = $("#dr_aux_title").val(); // 副标题
            var dr_sel_price = $("#dr_sel_price").val(); // 课程售价
            var dr_del_price = $("#dr_del_price").val(); // 市场价
            var load_img = $("#load_img").val(); // 课程封面
            var src = $(".update-word-add > img").attr("src"); // 修改图片
            var dr_content = $("#dr_content").val(); // 课程详细信息编辑

            console.log("dr_title "+dr_title);
            console.log("dr_aux_title "+dr_aux_title);
            console.log("dr_sel_price "+dr_sel_price);
            console.log("del_price "+dr_del_price);
            console.log("dr_content "+dr_content);


            if(dr_title&&dr_aux_title&&dr_sel_price&&dr_del_price&&dr_content){      // 如已填写基本信息
                if(load_img||src){
                    $('[type="submit"]').click();
                }
                else{
                    alert("请上传课程封面");
                }
            }
            else{
                alert("请填写完基本信息");
            }
        }
    });


});
